<template>
	<view>
		<ad unit-id="adunit-0daff0d411198872" ad-type="video" ad-theme="white"></ad>

		<view class="btn-info-order">
			<view class="tips" v-if="isSuccess">
				领取成功
			</view>
			<view v-else>
				<view class="tips" v-if="!isOk">
					签到免费领
				</view>
				<view class="tips" v-if="isOk">
					今日已领取，明日再来；
				</view>
			</view>

			<view class="bio" @click="goOrder" v-if="!isOk">
				<text  style="line-height: 18px;">立即领取</text>
				<image src="/static/vd.png" mode="widthFix" style="width: 26px;margin-left:7px;"></image>
			</view>
		</view>

	</view>
</template>

<script>
	let videoAd = null
	const app = getApp();
	export default {
		data() {
			return {
				isOk: false,
				isSuccess: false,
				userKey: null
			}
		},
		onLoad(e) {
			if (wx.createRewardedVideoAd) {
				videoAd = wx.createRewardedVideoAd({
					adUnitId: 'adunit-ad417c6c04eaeac2'
				})
				videoAd.onLoad(() => {})
				videoAd.onError((err) => {})
				videoAd.onClose((res) => {
					// 用户点击了【关闭广告】按钮
					if (res && res.isEnded) {
						// 正常播放结束，可以下发游戏奖励
						uni.request({
							url: app.globalData.serviceUrl +
								'/UserMiniProgramController/getGoods',

							method: 'POST',
							data: {
								key: this.userKey
							},
							header: {
								'content-type': 'application/x-www-form-urlencoded'
							},
							success: res => {
								var data = res.data;
								if (data.state == 1) {
									uni.showToast({
										title: "领取成功",
										icon: "success"
									})
									this.isSuccess = true;
									this.isOk = true;
								} else {
									uni.showToast({
										title: res.data.notice,
										icon: "none"
									})
								}
							}
						});

					} else {
						// uni.showToast({
						// 	title: "看完广告才能连接哦~",
						// 	icon: "none"
						// })
					}
				})
			}
			this.loginInit();
		},
		methods: {
			loginInit() {
				uni.login({
					success: (res) => {
						uni.request({
							url: app.globalData.serviceUrl + '/loge',
							data: {
								code: res.code
							},
							method: 'POST',
							header: {
								'content-type': 'application/x-www-form-urlencoded'
							},
							success: (res) => {
								if (res.data.state == 1) {
									this.userKey = res.data.notice;
									this.getIsOk();
								} else {
									uni.showToast({
										title: res.data.notice,
										icon: "none",
										duration: 2000
									});
								}

							},

						});

					}
				});
			},
			getIsOk() {
				uni.request({
					url: app.globalData.serviceUrl +
						'/UserMiniProgramController/getIsGoods',

					method: 'POST',
					data: {
						key: this.userKey
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: res => {
						console.log(res)
						if (res.data.state == 1) {
							this.isOk = res.data.data;
						} else {
							uni.showToast({
								title: res.data.notice,
								icon: "none"
							})
						}

					}
				});
			},
			goIndex() {
				uni.redirectTo({
					url: "/pages/newIndex/newIndex"
				})
			},
			goOrder() {
				if (videoAd) {
					videoAd.show().catch(() => {
						// 失败重试
						videoAd.load()
							.then(() => videoAd.show())
							.catch(err => {
								console.log('激励视频 广告显示失败')
							})
					})
				}

			},
		}
	}
</script>

<style scoped lang="scss">
	.btn-info-order {
		border-top: 1px solid #eee;
		padding-top: 20px;

		.tips {
			text-align: center;
			font-size: 14px;
			color: #666;
		}

		.bio {
			width: 200px;
			height: 40px;
			font-size: 18px;
			background: #333;
			color: #fff;
			border-radius: 5px;
			box-shadow: 7px 6px 28px 1px rgba(0, 0, 0, 0.24);
			margin: 20px auto;
			display: flex;
			align-items: center;
			justify-content: center;

			&:active {
				box-shadow: 7px 6px 28px 1px rgba(0, 0, 0, 0.24);
				transform: translateY(4px);
			}
		}
	}
</style>
